package com.dawning.utiming.task.fragment

import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.http.BaseHttpCallBack
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.activity.UnitReportDetailActivity
import cc.vv.btpartyjob.partymanager.adapter.RegisterAdapter
import cc.vv.btpartyjob.partymanager.api.PartyManagerAPI
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.RegisterFragmentDelegate
import cc.vv.btpartyjob.partymanager.moudel.req.UnitRegisterRequestObj
import cc.vv.btpartyjob.partymanager.moudel.resp.UnitRegisterNoResponseObj
import cc.vv.btpartyjob.partymanager.moudel.resp.UnitRegisterResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.mvp.binder.BaseDataBinder
import cc.vv.mvp.fragment.BaseFragmentMVP
import cc.vv.party.beans.vo.UnitVO
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 已报到业fragment
 * Created by Chen.qi on 2018/10/27.
 */
class UnitRegisterYesFragment : BaseFragmentMVP<RegisterFragmentDelegate>(),
        BaseQuickAdapter.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {
    /**
     * 适配器
     */
    private var mAdapter: RegisterAdapter? = null

    /**
     * 当前页数
     */
    private var pageNo: Int = 1
    /**
     * 每页条数
     */
    private val pageSize: Int = 10

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<UnitVO>? = null

    private var whetherRefresh = false

    /**
     * 区Id
     */
    private var area: String? = null

    /**
     * 街道id
     */
    private var streetId: String? = null

    /**
     * 社区id
     */
    private var communityId: String? = null

    override fun initData() {
        if (mAdapter == null) {
            mDataList = ArrayList()
            mAdapter = RegisterAdapter(R.layout.item_register_unit)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }
    }

    override fun onVisibilityChanged(visible: Boolean) {
        super.onVisibilityChanged(visible)
        if (visible && !whetherRefresh) {
            pageNo = 1
            val userInfo = UserInfoManageUtil.getUserInfo()
            val userOrgList = userInfo?.userOrgList
            if (null != userOrgList) {
                for (i in userOrgList.indices) {
                    when {
                        userOrgList[i].type == 0 -> area = userOrgList[i].id
                        userOrgList[i].type == 1 -> streetId = userOrgList[i].id
                        userOrgList[i].type == 2 -> communityId = userOrgList[i].id
                    }
                }
            }
            PartyManagerServer.unitRegisterListYes(area, streetId, communityId, "0", pageNo, pageSize, true)
            whetherRefresh = true
        }
    }


    override fun bindEvenListener() {
        mAdapter?.onItemClickListener = this
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)
        mAdapter?.disableLoadMoreIfNotFullPage()
    }

    override fun getDelegateClass(): Class<RegisterFragmentDelegate> {
        return RegisterFragmentDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val intent = Intent(activity, UnitReportDetailActivity::class.java)
        intent.putExtra("unitId", mDataList!![position].id)
        startActivity(intent)
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        if (whetherRefresh) {
            when (obj) {
                is UnitRegisterResponseObj -> {
                    viewDelegate?.stopRefreshing()
                    if (null == obj.data) {
                        return
                    }
                    if (1 == pageNo) {
                        mDataList?.clear()
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        viewDelegate?.setAdapterData(mAdapter, mDataList)
                        mAdapter?.disableLoadMoreIfNotFullPage()
                    } else {
                        if (mAdapter?.data?.size!! >= obj.data.total) {
                            //数据全部加载完毕
                            mAdapter?.loadMoreEnd(true)
                            mAdapter?.loadMoreEnd()
                        } else {
                            //成功获取更多数据
                            if (null != obj.data.records) {
                                mDataList!!.addAll(obj.data.records)
                            }
                            mAdapter?.setNewData(mDataList)
                            //加载数据
                            mAdapter?.disableLoadMoreIfNotFullPage()
                            mAdapter?.loadMoreComplete()
                        }
                    }
                }
            }
        }
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        mAdapter?.loadMoreComplete()
        mAdapter?.loadMoreFail()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }

    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        PartyManagerServer.unitRegisterListYes(area, streetId, communityId, "0", pageNo, pageSize, false)
    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        PartyManagerServer.unitRegisterListYes(area, streetId, communityId, "0", pageNo, pageSize, false)
    }

    /**
     * 组织选择后
     */
    fun chooseYesFragment(orgId: String?, type: String?) {
        pageNo = 1
        mDataList?.clear()
        when (type) {
            "0" -> {
                area = orgId
                streetId = ""
                communityId = ""
            }
            "1" -> {
                area = ""
                streetId = orgId
                communityId = ""
            }
            "2" -> {
                area = ""
                streetId = ""
                communityId = orgId
            }
        }

        val request = UnitRegisterRequestObj()
        request.area = area
        request.street = streetId
        request.community = communityId
        request.type = "0"
        request.page = "$pageNo"
        request.pageSize = "$pageSize"
        LKHttp.get(PartyManagerAPI.unitRegisterUrl, request, UnitRegisterNoResponseObj::class.java,
                object : BaseHttpCallBack<UnitRegisterNoResponseObj>() {
                    override fun onSuccess(url: String?, obj: UnitRegisterNoResponseObj?) {
                        super.onSuccess(url, obj)
                        if (1 == pageNo) {
                            mDataList?.clear()
                            if (null != obj?.data?.records) {
                                mDataList!!.addAll(obj.data.records)
                            }
                            viewDelegate?.setAdapterData(mAdapter, mDataList)
                            mAdapter?.disableLoadMoreIfNotFullPage()
                        }
                    }

                }, false)
    }

}